<template>
    <div>
        <el-dialog title="添加书籍" :visible.sync="dialogVisible" width="30%">
            <el-form :model="newBook" :rules="rules" ref="bookForm">
                <el-form-item label="书名" prop="title">
                    <el-input v-model="newBook.title"></el-input>
                </el-form-item>
                <el-form-item label="作者" prop="author">
                    <el-input v-model="newBook.author"></el-input>
                </el-form-item>
                <el-form-item label="库存" prop="copies">
                    <el-input-number v-model="newBook.copies" :min="1"></el-input-number>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="submitForm">确定</el-button>
            </span>
        </el-dialog>
        <el-table :data="tableData" style="width: 100%" max-height="600">
            <el-table-column fixed prop="id" label="ID" width="150">
            </el-table-column>
            <el-table-column prop="title" label="书名" width="150">
            </el-table-column>
            <el-table-column prop="author" label="作者" width="150">
            </el-table-column>
            <el-table-column prop="copies" label="库存">
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="150">
                <template slot-scope="scope">
                    <el-button @click.native.prevent="deleteRow(scope.$index, tableData)" type="text" size="small">
                        移除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-button @click="addRow" type="primary">添加书籍</el-button>
    </div>
</template>

<script>
export default {
    methods: {
        deleteRow(index, rows) {
            rows.splice(index, 1);
        },
        addRow() {
            this.dialogVisible = true;
            this.newBook = {
                title: '',
                author: '',
                copies: 1
            };
        },
        submitForm() {
            this.$refs.bookForm.validate((valid) => {
                if (valid) {
                    this.tableData.push({
                        id: this.tableData.length + 1,
                        title: this.newBook.title,
                        author: this.newBook.author,
                        copies: this.newBook.copies
                    });
                    this.dialogVisible = false;
                }
            });
        }
    },
    data() {
        return {
            dialogVisible: false,
            newBook: {
                title: '',
                author: '',
                copies: 1
            },
            rules: {
                title: [
                    { required: true, message: '请输入书名', trigger: 'blur' }
                ],
                author: [
                    { required: true, message: '请输入作者', trigger: 'blur' }
                ]
            },
            tableData: [
                { "id": 1, "title": "Python编程", "author": "John Doe", "copies": 4 },
                { "id": 2, "title": "历史的秘密", "author": "Jane Smith", "copies": 3 },
                { "id": 3, "title": "宇宙探索", "author": "Emily Brown", "copies": 2 },
                { "id": 4, "title": "健康生活", "author": "Mike Johnson", "copies": 5 },
                { "id": 5, "title": "数学之美", "author": "Sarah Lee", "copies": 4 },
                { "id": 6, "title": "艺术欣赏", "author": "David White", "copies": 3 },
                { "id": 7, "title": "编程入门", "author": "Alice Green", "copies": 2 },
                { "id": 8, "title": "科幻世界", "author": "Frank Black", "copies": 5 },
                { "id": 9, "title": "自然奇观", "author": "Grace Blue", "copies": 4 },
                { "id": 10, "title": "音乐之声", "author": "Tom Red", "copies": 3 }
            ]

        }
    }
}
</script>

<style scoped>
.el-dialog {
    border-radius: 8px;
}

.el-dialog__header {
    padding: 10px 10px 10px;
    border-bottom: 1px solid #ebeef5;
}

.el-dialog__body {
    padding: 10px;
}

.el-form-item {
    margin-bottom: 22px;
}

.dialog-footer {
    display: flex;
    justify-content: flex-end;
    padding-top: 10px;
}

.dialog-footer .el-button {
    margin-left: 10px;
}

</style>